package edu.arizona.arid.akshen.entity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 
 * @author Sumin Byeon
 */
public abstract class Entity implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5129212969202723226L;

	protected static Properties config;
	public static String dbhost = "jdbc:mysql://localhost:3306/mysql";
	public static String dbname = "";
	protected static String dbuser = "";
	protected static String dbpassword = "";

	protected static Connection conn;

	public Entity() {
	}

	protected static Connection prepareConnection() throws SQLException {
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		readConfigurations();

		if (conn == null) {
			conn = DriverManager.getConnection(String.format(
					"jdbc:mysql://%s:3306/%s?user=%s&password=%s", dbhost,
					dbname, dbuser, dbpassword));
		}

		return conn;
	}
	
	protected static void readConfigurations() {
		if(config == null) {
			config = new Properties();
			try {
				config.load(new FileInputStream(new File("lib/config.ini")));
				dbhost = config.getProperty("DB_HOST");
				dbname = config.getProperty("DB_NAME");
				dbuser = config.getProperty("DB_USER");
				dbpassword = config.getProperty("DB_PASSWORD");
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public boolean insert() throws SQLException {
		return false;
	}

	public boolean update() throws SQLException {
		return false;
	}

	public boolean delete() throws SQLException {
		return false;
	}

	public static boolean insert(Entity entity) throws SQLException {
		return false;
	}

	public static boolean update(Entity entity) throws SQLException {
		return false;
	}

	public static boolean delete(Entity entity) throws SQLException {
		return false;
	}
}
